import requests
import logging
from write_to_mongo import MongoClient

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s: %(message)s')

INDEX_URL = "https://spa1.scrape.center/api/movie/?limit={limit}&offset={offset}"
LIMIT = 10
DETAIL_URL = "https://spa1.scrape.center/api/movie/{id}"
TOTAL_PAGE = 10


def scrape_api(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.json()
        logging.error('get invalid status code %s while scraping %s',
                      response.status_code, url)
    except requests.RequestException as e:
        logging.error('error occurred while scraping %s, error: %s',
                      url, e, exc_info=True)


def scrape_index(offset):
    url = INDEX_URL.format(limit=LIMIT, offset=offset)
    return scrape_api(url)


def scrape_detail(id):
    url = DETAIL_URL.format(id=id)
    return scrape_api(url)

def main():
    with MongoClient() as mongo_client:
        for page in range(1, TOTAL_PAGE + 1):
            offset = (page - 1) * LIMIT
            result = scrape_index(offset)
            for item in result.get('results', []):
                id = item.get('id')
                detail = scrape_detail(id)
                mongo_client.insert_one('movies', detail)
                logging.info('successfully saved detail %s', detail['id'])

if __name__ == '__main__':
    main()
